package com.ch.dsms_backend.service;

import com.ch.dsms_backend.entity.AUser;
import com.ch.dsms_backend.repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private AdminRepository adminRepository;

    @Override
    public String register(AUser requestUser) {
        List<AUser> list=adminRepository.login(requestUser);
        if(list.size()>0)
            return "existed";
        else{
            if(adminRepository.register(requestUser)>0)
                return "yes";
            else
                return "no";
        }
    }

    @Override
    public String login(AUser requestUser, HttpSession session) {
        List<AUser> list=adminRepository.login(requestUser);
        if(list.size()>0){
            session.setAttribute("auser",list.get(0));
//            System.out.println("login: "+list.get(0).getAname());
            return "ok";
        }else
            return "no";
    }
}
